package com.yinhai.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.res.Resources.NotFoundException;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yinhai.activity.R;

public class DialogFactory {
	// two buttons, OK button and Cancel button
	public final static int DIALOG_TYPE_CONFIRM = 0;
	// only one button
	public final static int DIALOG_TYPE_TIP = DIALOG_TYPE_CONFIRM + 1;
	// no button
	public final static int DIALOG_TYPE_LOADING = DIALOG_TYPE_TIP + 1;

	public static Dialog getConfirmDialog(Activity activity, int titleId,
			int msgId, int cancelBtnStringId, int okBtnStringId,
			final View.OnClickListener cancelBtnEvent,
			final View.OnClickListener okBtnEvent) {
		String title = null;
		try {
			title = activity.getResources().getString(titleId);
		} catch (NotFoundException e) {

		}
		String msg = null;
		try {
			msg = activity.getResources().getString(msgId);
		} catch (NotFoundException e) {

		}
		String cancelBtnString = null;
		try {
			cancelBtnString = activity.getResources().getString(
					cancelBtnStringId);
		} catch (NotFoundException e) {

		}
		String okBtnString = null;
		try {
			okBtnString = activity.getResources().getString(okBtnStringId);
		} catch (NotFoundException e) {

		}
		return getConfirmDialog(activity, title, msg, cancelBtnString,
				okBtnString, cancelBtnEvent, okBtnEvent);
	}

	public static Dialog getConfirmDialog(Activity activity, String title,
			String msg, String cancelBtnString, String okBtnString,
			final View.OnClickListener cancelBtnEvent,
			final View.OnClickListener okBtnEvent) {

		final Dialog dialog = new Dialog(activity, R.style.Theme_CustomDialog);
		final View dialogview = LayoutInflater.from(activity).inflate(
				R.layout.dialog_common, null);
		dialog.setContentView(dialogview);

		TextView titleView = (TextView) dialogview
				.findViewById(R.id.dialog_title);
		if (title == null) {
			titleView.setVisibility(View.GONE);
		} else {
			titleView.setText(title);
		}

		TextView msgTextView = (TextView) dialogview
				.findViewById(R.id.dialog_msg);
		if (msg != null) {
			msgTextView.setText(msg);
		}

		Button okButton = (Button) dialogview.findViewById(R.id.dialog_ok_btn);
		if (!TextUtils.isEmpty(okBtnString)) {
			okButton.setText(okBtnString);
		}
		okButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (okBtnEvent != null) {
					okBtnEvent.onClick(v);
				}
				dialog.dismiss();
			}
		});

		final Button cancelButton = (Button) dialogview
				.findViewById(R.id.dialog_cancel_btn);
		if (!TextUtils.isEmpty(cancelBtnString)) {
			cancelButton.setText(cancelBtnString);
		}
		cancelButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (cancelBtnEvent != null) {
					cancelBtnEvent.onClick(v);
				}
				dialog.dismiss();
			}
		});
		dialog.setCancelable(true);

		return dialog;
	}

	public static Dialog getConfirmDialog2(Activity activity, String title,
			String msg, String cancelBtnString, String okBtnString,
			final View.OnClickListener cancelBtnEvent,
			final View.OnClickListener okBtnEvent) {

		final Dialog dialog = new Dialog(activity, R.style.Theme_CustomDialog);
		final View dialogview = LayoutInflater.from(activity).inflate(
				R.layout.dialog_common2, null);
		dialog.setContentView(dialogview);

		TextView titleView = (TextView) dialogview
				.findViewById(R.id.dialog_title);
		if (title == null) {
			titleView.setVisibility(View.GONE);
		} else {
			titleView.setText(title);
		}

		TextView msgTextView = (TextView) dialogview
				.findViewById(R.id.dialog_msg);
		if (msg != null) {
			msgTextView.setText(msg);
		}

		TextView okButton = (TextView) dialogview
				.findViewById(R.id.dialog_ok_btn);
		if (!TextUtils.isEmpty(okBtnString)) {
			okButton.setText(okBtnString);
		}
		okButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (okBtnEvent != null) {
					okBtnEvent.onClick(v);
				}
				dialog.dismiss();
			}
		});

		final TextView cancelButton = (TextView) dialogview
				.findViewById(R.id.dialog_cancel_btn);
		if (!TextUtils.isEmpty(cancelBtnString)) {
			cancelButton.setText(cancelBtnString);
		}
		cancelButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (cancelBtnEvent != null) {
					cancelBtnEvent.onClick(v);
				}
				dialog.dismiss();
			}
		});
		dialog.setCancelable(true);

		return dialog;
	}

	public interface onClickListener {
		public boolean onClick();
	}

	public static Dialog getConfirmEditDialog2(Activity activity, View view,
			String title, String msg, String cancelBtnString,
			String okBtnString, final onClickListener cancelBtnEvent,
			final onClickListener okBtnEvent) {

		final Dialog dialog = new Dialog(activity, R.style.Theme_CustomDialog);

		dialog.setContentView(view);

		TextView titleView = (TextView) view.findViewById(R.id.dialog_title);
		if (title == null) {
			titleView.setVisibility(View.GONE);
		} else {
			titleView.setText(title);
		}

		TextView okButton = (TextView) view.findViewById(R.id.dialog_ok_btn);
		if (!TextUtils.isEmpty(okBtnString)) {
			okButton.setText(okBtnString);
		}
		okButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (okBtnEvent != null) {
					if (okBtnEvent.onClick()) {
						dialog.dismiss();
					}
				}

			}
		});

		final TextView cancelButton = (TextView) view
				.findViewById(R.id.dialog_cancel_btn);
		if (!TextUtils.isEmpty(cancelBtnString)) {
			cancelButton.setText(cancelBtnString);
		}
		cancelButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (cancelBtnEvent != null) {
					cancelBtnEvent.onClick();
				}
				dialog.dismiss();
			}
		});
		dialog.setCancelable(true);

		return dialog;
	}

	public interface ChangerEditDialogCallback {
		boolean okEvent(View v, String editContent);

		boolean cancelEvent(View v);
	}

	public static Dialog getTipDialog(Activity activity, String title,
			String msg, String btnString, final View.OnClickListener btnEvent) {

		final Dialog dialog = new Dialog(activity, R.style.Theme_CustomDialog);
		final View dialogview = LayoutInflater.from(activity).inflate(
				R.layout.dialog_common, null);
		dialog.setContentView(dialogview);

		TextView titleView = (TextView) dialogview
				.findViewById(R.id.dialog_title);
		if (TextUtils.isEmpty(title)) {
			titleView.setVisibility(View.GONE);
		} else {
			titleView.setText(title);
		}

		TextView msgTextView = (TextView) dialogview
				.findViewById(R.id.dialog_msg);
		if (!TextUtils.isEmpty(msg)) {
			msgTextView.setText(msg);
		}

		Button okButton = (Button) dialogview.findViewById(R.id.dialog_ok_btn);
		if (!TextUtils.isEmpty(btnString)) {
			okButton.setText(btnString);
		}
		okButton.setBackgroundResource(R.drawable.btn_bottom_left_right_radius);
		okButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (btnEvent != null) {
					btnEvent.onClick(v);
				}
				dialog.dismiss();
			}
		});

		Button cancelButton = (Button) dialogview
				.findViewById(R.id.dialog_cancel_btn);
		cancelButton.setVisibility(View.GONE);
		return dialog;
	}

	public static Dialog getConfirmDialog(Activity activity, String title,
			String msg, boolean isCenter, String cancelBtnString,
			String okBtnString, final View.OnClickListener cancelBtnEvent,
			final View.OnClickListener okBtnEvent) {

		final Dialog dialog = new Dialog(activity, R.style.Theme_CustomDialog);
		final View dialogview = LayoutInflater.from(activity).inflate(
				R.layout.dialog_common, null);
		dialog.setContentView(dialogview);

		TextView titleView = (TextView) dialogview
				.findViewById(R.id.dialog_title);
		if (title == null) {
			titleView.setVisibility(View.GONE);
		} else {
			titleView.setText(title);
		}

		TextView msgTextView = (TextView) dialogview
				.findViewById(R.id.dialog_msg);
		if (msg != null) {
			msgTextView.setText(msg);
		}

		if (isCenter) {
			msgTextView.setGravity(Gravity.CENTER);
		}

		Button okButton = (Button) dialogview.findViewById(R.id.dialog_ok_btn);
		if (!TextUtils.isEmpty(okBtnString)) {
			okButton.setText(okBtnString);
		}
		okButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (okBtnEvent != null) {
					okBtnEvent.onClick(v);
				}
				dialog.dismiss();
			}
		});

		final Button cancelButton = (Button) dialogview
				.findViewById(R.id.dialog_cancel_btn);
		if (!TextUtils.isEmpty(cancelBtnString)) {
			cancelButton.setText(cancelBtnString);
		}
		cancelButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (cancelBtnEvent != null) {
					cancelBtnEvent.onClick(v);
				}
				dialog.dismiss();
			}
		});
		dialog.setCancelable(true);

		return dialog;
	}

}